#include "listStack.h"
#include <stdlib.h>
#include <stdio.h>

ListStack_t *createListStack() {
    ListStack_t *stack=NULL;
    stack =malloc(sizeof(ListStack_t));
    if(stack==NULL) {
       fprintf(stderr,"Error allocating memory for stack");
        return NULL;
    }
    stack->top=NULL;
    stack->count=0;
    return stack;
}

void releaseListStack(ListStack_t *stack) {
    if(stack){
        while(stack->top){
            Node_t *temp=stack->top;
            stack->top=temp->next;
            free(temp);
        }
        free(stack);
        stack=NULL;
    }
}

int pushListStack(ListStack_t *stack, Element_t value) {
    if(stack==NULL) {
        printf("Error stack is NULL");
        return -1;
    }
    Node_t *node=NULL;
    node=malloc(sizeof(Node_t));
    if(node==NULL) {
        fprintf(stderr,"Error allocating memory for node");
        return -1;
    }
    node->val=value;
    node->next=stack->top;
    stack->top=node;
    stack->count++;
    return 0;
}

int popListStack(ListStack_t *stack) {
    if(stack==NULL) {
        printf("Error stack is NULL\n");
        return -1;
    }
    if(stack->count==0) {
        printf("pop Error stack is empty\n");
        return -1;
    }
    Node_t *node=stack->top;
    stack->top=node->next;
    stack->count--;
    free(node);
    return 0;
}

Element_t getTopListStack(ListStack_t *stack) {
    if(stack==NULL) {
        printf("Error stack is NULL");
        return -1;
    }
    if(stack->count==0) {
        printf("getTop Error stack is empty");
        return -1;
    }
    return stack->top->val;
}

bool isEmptyListStack(ListStack_t *stack) {
    if(stack==NULL) {
        printf("Error stack is NULL");
        return -1;
    }
    return stack->count==0;
}




